﻿/*****
    用于排列对象，对象必须添加Boxcollider脚本，通过Boxcollider的大小+callwidth、callheigth的宽高进行排列
    可进行纵横向 n*m的排列，在删除对象重新排列之前需要将对象active设为false；
*****/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MyGrid2 : MonoBehaviour
{
    [SerializeField]
    private UIPanel _uiPanel=null;
    public enum Arrangement
    {
        Horizontal,
        Vertical,
    }

    public static int num = 0;
    public float CallWidth = 200;
    public float CallHeidth = 200;
    public int Line = 1;

    //排列方式
    public Arrangement arrangement = Arrangement.Horizontal;

    public bool hideInactive = false;

    public List<Transform> tranlist = new List<Transform>();

    private List<BoxCollider> boxlit = new List<BoxCollider>();

    public bool Desc = false;

    private float InitLocalX;
    private float InitLocalY;
    // Use this for initialization
    void Start()
    {
       // Debug.Log("MyGrid Start");
        ResetPosition();
        Messenger.AddListener<GameObject>("BlackGroundChange", BlackGroundChange);
    }
    public void GetChildList()
    {
        tranlist.Clear();
        boxlit.Clear();
        Transform myTrans = transform;
        if (Desc)
        {
            for (int i = myTrans.childCount - 1; i > -1; --i)
            {
                Transform t = myTrans.GetChild(i);
                if (!hideInactive || (t && NGUITools.GetActive(t.gameObject)))
                {
                    BoxCollider _boxCollider = t.GetComponent<BoxCollider>();
                    if (_boxCollider == null)
                    {
                        continue;
                    }
                    boxlit.Add(t.GetComponent<BoxCollider>());
                    tranlist.Add(t);
                 //   Debug.Log(t.name);
                }
            }
        }
        else
        {
          //  Debug.Log("myTrans.childCount======" + myTrans.childCount);
            for (int i = 0; i < myTrans.childCount; ++i)
            {
                Transform t = myTrans.GetChild(i);
                if (!hideInactive || (t && NGUITools.GetActive(t.gameObject)))
                {
                    BoxCollider _boxCollider = t.GetComponent<BoxCollider>();
                    if (_boxCollider == null)
                    {
                        continue;
                    }
                    boxlit.Add(t.GetComponent<BoxCollider>());
                    tranlist.Add(t);
                   // Debug.Log(t.name);
                }
            }
        }
    }

    public void ResetPosition()
    {
        GetChildList();
        if (_uiPanel != null)
        {
            _uiPanel.clipOffset = new Vector2(0, 0);
            _uiPanel.transform.localPosition = Vector3.zero;
            _uiPanel.Update();
        }
            

        Vector3 pos = new Vector3();
        Transform myTrans = null;


        if (tranlist.Count > 0)
        {
            tranlist[0].localPosition = Vector3.zero;
            InitLocalX = tranlist[0].localPosition.x;
            InitLocalY = tranlist[0].localPosition.y;
            if (arrangement == Arrangement.Horizontal)
            {

                for (int i = 1; i < tranlist.Count; i++)
                {
                    myTrans = tranlist[i];
                    pos = tranlist[i - 1].localPosition;
                    pos.x = pos.x + CallWidth + boxlit[i - 1].size.x;

                    myTrans.localPosition = pos;
                  //  Debug.Log(myTrans.name + " = " + pos.x);
                    int Hnum = i % Line;
                    int Wnum = i - Line;

                    myTrans = tranlist[i];

                    if (Wnum < 0)
                    {
                        pos = tranlist[i - 1].localPosition;
                        pos.x = InitLocalX;
                    }
                    else
                    {
                        pos = tranlist[Wnum].localPosition;
                        //pos.x = pos.x + CallWidth + boxlit[Hnum].size.x * myTrans.localScale.x;
                        pos.x = pos.x + CallWidth + (boxlit[Hnum].size.x +boxlit[i].size.x);
                    }

                    pos.y = InitLocalY - Hnum * (CallHeidth + boxlit[i - 1].size.y * myTrans.localScale.y);
                    pos.z = 0;
                    myTrans.localPosition = pos;
                }

            }
            else if (arrangement == Arrangement.Vertical)
            {

                for (int i = 1; i < tranlist.Count; i++)
                {
                    int Wnum = i % Line;
                    int Hnum = i - Line;

                    myTrans = tranlist[i];

                    if (Hnum < 0)
                    {
                        pos = tranlist[i - 1].localPosition;
                        pos.y = InitLocalY;
                    }
                    else
                    {
                        pos = tranlist[Hnum].localPosition;
                        //pos.y = pos.y - CallHeidth - boxlit[Hnum].size.y * myTrans.localScale.y;
                        pos.y = pos.y - CallHeidth - (boxlit[Hnum].size.y/2+ boxlit[i].size.y/2);
                    }

                    pos.x = InitLocalX + Wnum * (CallWidth + boxlit[i - 1].size.x * myTrans.localScale.x);
                    pos.z = 0;
                    myTrans.localPosition = pos;
                   // Debug.Log("Mygrid------" + i + "  " + pos);
                }
            }
        }
    }


    //	void OnValidate () { if (!Application.isPlaying && NGUITools.GetActive(this)) ResetPosition(); }
    private void BlackGroundChange(GameObject obj)
    {
        Debug.Log("receive BlackGroundChange" + num);
        ResetPosition();
        //		Messenger.Broadcast<string>("MessageBoxError","BlackGroundChange！");
        num++;
    }

    void OnDestroy()
    {
        try
        {
            Messenger.RemoveListener<GameObject>("BlackGroundChange", BlackGroundChange);
        }
        catch
        {
        }

       // Debug.Log("ONdestory");
    }
}








